package com.sgb.kjwl.viewmodel.self.company

import android.app.Activity
import android.text.TextUtils
import com.sgb.kjwl.api.APIManager
import com.sgb.kjwl.api.NewAPIManager
import com.sgb.kjwl.api.service.IMServiceAPI
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.H5ReEntity
import com.swgk.core.dialog.DialogHelper
import com.swgk.core.util.MToast
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class QuotationViewModel : BaseViewModel() {


    // 询价详情
    fun getPriceCheckDetail(activity: Activity?, id: String, state: String?) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false)
        NewAPIManager.getInstance().productInquiryAPI.getUrlSetting("pensonalEnpuiryDetail").enqueue(object : ICallBack<BaseEntity<IMServiceAPI.GetUrlSettingEntity?>?>() {
            override fun onSuccess(data: BaseEntity<IMServiceAPI.GetUrlSettingEntity?>?) {
                val url = data?.data?.url + "?no=" + id
                showUrl(activity, url, id, "1", state)
            }
        })
    }

    fun showUrl(activity: Activity?, url: String?, id: String?, type: String?, state: String?) {
        WebViewX5HostActivity.start(activity, url, id, type, state)
    }


    //报价详情
    fun getDataInfo(activity: Activity?, note: String, position: Int) {
        val s = if (position == 0) "pensonalEnpuiryDetail" else "pensonalQuoteDetail"
        APIManager.getInstance().lookingforAJobAPI.getUrlSetting(s).enqueue(object : Callback<BaseEntity<H5ReEntity>?> {
            override fun onResponse(call: Call<BaseEntity<H5ReEntity>?>, response: Response<BaseEntity<H5ReEntity>?>) {
                if (response.body() == null || response.body()!!.state == null) {
                    return
                }
                if (response.body()!!.state == APIManager.RESPONSE_OK) {
                    if (!TextUtils.isEmpty(response.body()!!.data.url)) {
                        val url = response.body()!!.data.url + "?no=" + note
                        showUrl(activity, url)
                    } else {
                        showUrl(activity, "")
                    }
                } else {
                    MToast.showToast(activity, response.body()!!.msg)
                }
            }

            override fun onFailure(call: Call<BaseEntity<H5ReEntity>?>, t: Throwable) {}
        })
    }

    //    跳转到H5界面
    fun showUrl(activity: Activity?, url: String?) {
        WebViewX5HostActivity.start(activity, url)
    }


}